#!/bin/bash
go env
GOROOT_COPY="$GOROOT"copy

echo $GOROOT
echo $GOROOT_COPY

# copy go root to another directory
sudo cp -rf -v $GOROOT $GOROOT_COPY

# remove old version
cd $GOROOT/src

sudo rm -rf -v $GOROOT/src/chainmaker/
sudo rm -rf -v $GOROOT_COPY/src/chainmaker/
sudo rm -rf -v $GOROOT/pkg/linux_amd64/chainmaker/
sudo rm -rf -v $GOROOT_COPY/pkg/linux_amd64/chainmaker/

## clone mock sdk
echo $GOROOT/src
cd $GOROOT/src
sudo git clone -b v2.2.0_alpha_qc https://git.code.tencent.com/ChainMaker/contract-sdk-docker-go-ide.git chainmaker

# remove no use file
sudo rm -rf -v $GOROOT/src/chainmaker/.git/
sudo rm -rf -v $GOROOT/src/chainmaker/go.mod $GOROOT/src/chainmaker/go.sum
sudo rm -rf -v $GOROOT/src/chainmaker/pb/proto/
sudo rm -v $GOROOT/src/chainmaker/pb/Makefile

# replace chainmaker-contract-sdk-docker-go to chainmaker
sudo sed -i "" "s/chainmaker.org\\/chainmaker\\/chainmaker-contract-sdk-docker-go/chainmaker/g" $(grep chainmaker-contract-sdk-docker-go -rl $GOROOT/src/chainmaker)

# clone actual sdk
echo $GOROOT_COPY/src
cd $GOROOT_COPY/src
sudo git clone -b v2.2.0_alpha_qc https://git.code.tencent.com/ChainMaker/chainmaker-contract-sdk-docker-go chainmaker

# remove no use file
sudo rm -rf -v $GOROOT_COPY/src/chainmaker/.git/
sudo rm -rf -v $GOROOT_COPY/src/chainmaker/demo/
sudo rm -v $GOROOT_COPY/src/chainmaker/go.mod $GOROOT_COPY/src/chainmaker/go.sum $GOROOT_COPY/src/chainmaker/README.md
sudo rm -v $GOROOT_COPY/src/chainmaker/main.go $GOROOT_COPY/src/chainmaker/Makefile $GOROOT_COPY/src/chainmaker/build.sh
sudo rm -v $GOROOT_COPY/src/chainmaker/.gitignore
sudo rm -rf -v $GOROOT_COPY/src/chainmaker/pb/proto/

# replace chainmaker-contract-sdk-docker-go to chainmaker
sudo sed -i "" "s/chainmaker.org\\/chainmaker-contract-sdk-docker-go/chainmaker/g" $(grep chainmaker-contract-sdk-docker-go -rl $GOROOT_COPY/src/chainmaker)

# install new module
cd $GOROOT/src
sudo go env -w GO111MODULE="on"
sudo go env -w GOPROXY=https://goproxy.cn,direct
sudo go mod tidy
sudo go mod vendor
sudo go env -w GO111MODULE="off"
cd $GOROOT/src
sudo go install -i -a ./chainmaker/...
sudo go env -w GO111MODULE="on"

cd $GOROOT_COPY/src
sudo GOROOT=$GOROOT_COPY go env -w GO111MODULE="on"
sudo GOROOT=$GOROOT_COPY go env -w GOPROXY=https://goproxy.cn,direct
sudo GOROOT=$GOROOT_COPY go mod tidy
sudo GOROOT=$GOROOT_COPY go mod vendor
sudo GOROOT=$GOROOT_COPY go env -w GO111MODULE="off"
sudo GOROOT=$GOROOT_COPY cd $GOROOT_COPY/src
sudo GOROOT=$GOROOT_COPY go install -i -a ./chainmaker/...
sudo GOROOT=$GOROOT_COPY go env -w GO111MODULE="on"

exit
